package com.gdut.multiusercommunicationsystem.controller;

import com.gdut.multiusercommunicationsystem.mapper.SearchMapper;
import com.gdut.multiusercommunicationsystem.model.User;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class SearchController {
    @Resource
    public SearchMapper searchMapper;

    @GetMapping("/getFriend")
    @ResponseBody
    public Object getFriend(String text, HttpServletRequest request){
        HttpSession httpSession=request.getSession(false);
        User user=(User) httpSession.getAttribute("user");
        List<User> friendNameList=searchMapper.getFriendNameByUserName(text,user.getUserId());
        String myself=user.getUsername();
        for(int i=0;i<friendNameList.size();i++){
            if(myself.equals(friendNameList.get(i).getUsername())){
                friendNameList.remove(i);
            }
            friendNameList.get(i).setPassword("");
        }
        return friendNameList;
    }
}
